package winOrDie.Net.Server;

import java.util.ArrayList;

import winOrDie.Net.Client.WoD_Peer;
import winOrDie.Net.Client.WoD_Peers_List;


public class WoD_Server_Client_List {

	
	/**
	 * The {@link WoD_Server_Client} {@link ArrayList}
	 */
	private ArrayList<WoD_Server_Client> clients = new ArrayList<WoD_Server_Client>();
	
	
	public WoD_Server_Client_List() {
	}
	
	/**
	 * Constructor from {@link WoD_Peers_List} object 
	 * @param woDPeersList
	 */
	public WoD_Server_Client_List(WoD_Peers_List woDPeersList) {
		for (WoD_Peer woDPeer : woDPeersList.getWoD_Peers()) {
			clients.add(new WoD_Server_Client(woDPeer));
		}
	}
	
	
	public ArrayList<WoD_Server_Client> getClients() {
		return clients;
	}

	public void add_client(WoD_Server_Client client)
	{
		clients.add(client);
	}
	
	public void removeClient_By_TCP_Port(
			String clientDirecction, 
			int tcp_port) 
	throws Exception
	{
		int position = get_positionOfClient_By_TCP_Port(clientDirecction, tcp_port);
		clients.remove(position);
	}
	
	public void remove_Client_By_P2P_ID(String P2P_ID)
	{
		int positon = get_positionOfClient_By_ScribeNode_ID(P2P_ID);
		if (positon != -1)
		{
			clients.remove(positon);
		}
	}
	
	
	/**
	 * This public method returns client defined by parameters
	 * @param clientDirecction
	 * @param udp_port
	 * @return {@link WoD_Server_Client}
	 * @throws Exception
	 */
	public WoD_Server_Client get_Client_By_UDP_Port(
			String clientDirecction, 
			int udp_port) 
	throws Exception
	{
		return clients.get(get_positionOfClient_By_UDP_Port(clientDirecction, udp_port)); 
	}
	
	/**
	 * This public method returns client position defined by parameters
	 * @param clientDirection
	 * @param udp_port
	 * @return int
	 * @throws Exception
	 */
	public int get_positionOfClient_By_UDP_Port(
			String clientDirection, 
			int udp_port) 
	throws Exception
	{
		
		for (int position = 0; position < clients.size(); position++) {
			if (clients.get(position).getHostDirection().equals(clientDirection) && (clients.get(position).getUdp_port() == udp_port))
			{
				return position;
			}
			
		}

		Exception exception = new Exception("Client with direcction " + clientDirection + " and udp port: " + udp_port + " not found");
		throw exception;
	}
	
	/**
	 * This public method returns client position defined by parameters
	 * @param scribeNode_ID
	 * @return int
	 * @throws Exception
	 */
	public int get_positionOfClient_By_ScribeNode_ID(String scribeNode_ID)
	{
		for (int position = 0; position < clients.size(); position++) {
			
			
			if (clients.get(position).getScribeNode_ID().equals(scribeNode_ID))
			{
				return position;
			}
			
		}
		
		return -1;
	}
	/**
	 * This public method returns client position defined by parameters
	 * @param clientDirection
	 * @param udp_port
	 * @return int
	 * @throws Exception
	 */
	public int get_positionOfClient_By_TCP_Port(
			String clientDirection, 
			int tcp_port) 
	throws Exception
	{
		for (int position = 0; position < clients.size(); position++) {
			if (clients.get(position).getHostDirection().equals(clientDirection) && (clients.get(position).getTcp_Socket().getPort() == tcp_port))
			{
				return position;
			}
			
		}
		
		Exception exception = new Exception("Client with direcction " + clientDirection + " and tcp port: " + tcp_port + " not found");
		throw exception;
		
	}
	
	
	/**
	 * This public method returns client position defined by parameters
	 * @param userName
	 * @return int
	 */
	public int get_positionOfClient_By_UserName(String userName)
	{
	
		for (int position = 0; position < clients.size(); position++) {
			if (clients.get(position).getUserName().toLowerCase().equals(userName.toLowerCase()))
			{
				return position;
			}
		}
		return -1;
	}
	
	/**
	 * This public method returns client position defined by parameters
	 * @param ID
	 * @return
	 */
	public int get_PositionOfClient_By_ID(int ID)
	{
		for (int position = 0; position < clients.size(); position++) {
			if (ID == clients.get(position).getCliend_Id())
			{
				return position;
			}
		}
		return -1;
	}
	
}
